package com.ysd.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBManager {
	private final static String DRIVER = "com.mysql.jdbc.Driver";
	private final static String URL = "jdbc:mysql://localhost:3306/guofengmeizhuang";
	private final static String USER = "root";
	private final static String PWO = "admin";
	//  取    
	public static Connection grtConnection() {
		try {
			//1.        
			Class.forName(DRIVER);
			//2.        
			Connection conn = DriverManager.getConnection(URL, USER,PWO);
			return conn;//             蚍祷     
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;//        斐�  蚍祷 null
	}
	//执  SQL   
	public static int executeUpdate(String sql) {
		Connection conn = grtConnection();
		Statement st = null;
		try {
			//3.    Statement    
			st =conn.createStatement();
			//4.执  SQL   
			int n=st.executeUpdate(sql);
			return n;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}finally {
			closeAll(conn,st,null);
		}
	}
	public static void closeAll(Connection conn,Statement st,ResultSet rs) {
		try {
			//6. 乇   
			if(rs!=null) {
				rs.close();
			}
			if(st!=null) {
				st.close();
			}
			if(conn!=null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();//  印      息
		}
	}
}